package CustomClassFile.CronJob;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.interfaces.schedule.CronJob;

public class SyncZswsShiftType extends BaseCronJob implements CronJob {

	// 同步排班组数据
	public void execute() {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		List<List> insertlist = new ArrayList<List>();
		List<List> updatelist = new ArrayList<List>();
		JSONObject jsonObject = new JSONObject();
		weaver.interfaces.datasource.DataSource ds = (weaver.interfaces.datasource.DataSource) StaticObj
				.getServiceByFullname(("datasource.HRDB"), weaver.interfaces.datasource.DataSource.class);
		java.sql.Connection conn = ds.getConnection();
		int datalength = 0;
		int update = 0;
		int insert = 0;
		try {

			String sql = "select * from zswsHRaShift_type";
			PreparedStatement preparedStatement = conn.prepareStatement(sql);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				RecordSet rss = new RecordSet();
				String sqls = "select * from uf_pbzxx where shift=?";
				rss.executeQuery(sqls, rs.getString("Shift"));
				if (rss.next()) {
					List<String> updateparam = new ArrayList<String>();
					updateparam.add(rs.getString("card1"));
					updateparam.add(rs.getString("card2"));
					updateparam.add(rs.getString("card3"));
					updateparam.add(rs.getString("card4"));
					updateparam.add(rs.getString("CardTimes"));
					updateparam.add(rs.getString("endT1"));
					updateparam.add(rs.getString("endT2"));
					updateparam.add(rs.getString("endT3"));
					updateparam.add(rs.getString("endT4"));
					updateparam.add(rs.getString("IsDisabled"));
					updateparam.add(rs.getString("scan1"));
					updateparam.add(rs.getString("scan2"));
					updateparam.add(rs.getString("scan3"));
					updateparam.add(rs.getString("scan4"));
					updateparam.add(rs.getString("Shift"));
					updateparam.add(rs.getString("Title"));
					updateparam.add(rs.getString("xHours"));
					updateparam.add("update:"+DateUtil.getCurrentDateTime(null));
					updateparam.add(rss.getString("id"));
					updatelist.add(updateparam);
					update++;
				} else {
					List<String> insertparam = new ArrayList<String>();
					insertparam.add(rs.getString("card1"));
					insertparam.add(rs.getString("card2"));
					insertparam.add(rs.getString("card3"));
					insertparam.add(rs.getString("card4"));
					insertparam.add(rs.getString("CardTimes"));
					insertparam.add(rs.getString("endT1"));
					insertparam.add(rs.getString("endT2"));
					insertparam.add(rs.getString("endT3"));
					insertparam.add(rs.getString("endT4"));
					insertparam.add(rs.getString("IsDisabled"));
					insertparam.add(rs.getString("scan1"));
					insertparam.add(rs.getString("scan2"));
					insertparam.add(rs.getString("scan3"));
					insertparam.add(rs.getString("scan4"));
					insertparam.add(rs.getString("Shift"));
					insertparam.add(rs.getString("Title"));
					insertparam.add(rs.getString("xHours"));
					insertparam.add("insert:"+DateUtil.getCurrentDateTime(null));
					insertlist.add(insertparam);
					insert++;
				}
				datalength++;
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		RecordSet rs1 = new RecordSet();
		String insertbatch = "insert into uf_pbzxx(card1,card2,card3,card4,CardTimes,endT1,endT2,endT3,endT4,IsDisabled,scan1,scan2,scan3,scan4,Shift,Title,xHours,remarks) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		rs1.executeBatchSql(insertbatch, insertlist);

		RecordSet rs2 = new RecordSet();
		String updatebatch = "update uf_pbzxx set card1=?,card2=?,card3=?,card4=?,CardTimes=?,endT1=?,endT2=?,endT3=?,endT4=?,IsDisabled=?,scan1=?,scan2=?,scan3=?,scan4=?,Shift=?,Title=?,xHours=?,remarks=? where id=?";
		rs2.executeBatchSql(updatebatch, updatelist);
		jsonObject.put("total", datalength);
		jsonObject.put("insert", insert);
		jsonObject.put("update", update);
		long endTime = System.currentTimeMillis();
		long usetime = (endTime - startTime) / 1000;
		jsonObject.put("usetime", usetime + "s");
		
		BaseBean writelog = new BaseBean();
		writelog.writeLog("排班组同步"+jsonObject.toString());
	}
}
